package cn.kgc.service.imp;

import cn.kgc.dao.BackendUserDao;
import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.BackendUser;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.service.BackendUserService;
import cn.kgc.until.PageSupport;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class BackendUserServiceImpl  implements BackendUserService {

    @Resource
    private BackendUserDao backendUserDao;

    /**
     * 后台用户登录
     * @param userCode 用户名
     * @param userPassword 密码
     * @return 判断用户名与密码是否正确
     */
    @Override
    public BackendUser findByUser(String userCode, String userPassword) {
        return backendUserDao.findByUser(userCode,userPassword);
    }

    /**
     * 分页与查询
     * @param querySoftwareName
     * @param queryFlatformId
     * @param pageIndex
     * @param queryCategoryLevel1
     * @param queryCategoryLevel2
     * @param queryCategoryLevel3
     * @return
     */
    @Override
    public HashMap<String, Object> findBackendUser(String querySoftwareName, Integer queryFlatformId, Integer pageIndex, Integer queryCategoryLevel1, Integer queryCategoryLevel2, Integer queryCategoryLevel3) {
        //实现分页+查询方法

        // 供应商列表
        List<DataDictionary> type=backendUserDao.findType();
        //一级查询
        List<AppCategory> fristFind=backendUserDao.fristFind();
        //设置分页
        //判断当前页
        if (pageIndex==null ||"".equals(pageIndex)){
            pageIndex=1;
        }
        //设置每页展现的数量
        int pageSize=5;
        //获取当前下标
        int pageNo=(pageIndex-1)*pageSize;
        //查询总行数
        HashMap<String,Object> find=new HashMap<>();
        find.put("querySoftwareName",querySoftwareName);
        find.put("queryFlatformId",queryFlatformId);
        find.put("queryCategoryLevel1",queryCategoryLevel1);
        find.put("queryCategoryLevel2",queryCategoryLevel2);
        find.put("queryCategoryLevel3",queryCategoryLevel3);
        find.put("pageSize",pageSize);
        find.put("pageNo",pageNo);

        int i =backendUserDao.allPageNo(find);
        //调用分页工具类
        PageSupport pageSupport=new PageSupport();
        pageSupport.setPageSize(pageSize);
        pageSupport.setCurrentPageNo(pageIndex);
        pageSupport.setTotalCount(i);
        //总页数!!!
        int totalPageCount=pageSupport.getTotalPageCount();

        //模糊查询!!!
        List<AppInfo> appInfo=backendUserDao.findByUserSome(find);

        //封装数据
        HashMap<String,Object> rel=new HashMap<>();
        rel.put("queryCategoryLevel1",fristFind);
        rel.put("type",type);
        rel.put("findSome",appInfo);
        rel.put("totalPageCount",totalPageCount);
        rel.put("totalCount",i);
        rel.put("currentPageNo",pageIndex);


        return rel;
    }

    /**
     * 二级列表判断
     * @param queryCategoryLevel1
     * @return
     */
    @Override
    public List<AppCategory> categoryLevelList(Integer queryCategoryLevel1) {
        return backendUserDao.categoryLevelList(queryCategoryLevel1);
    }

    /**
     * 三级列表
     * @param queryCategoryLevel2
     * @return
     */
    @Override
    public List<AppCategory> categoryLevelList2(Integer queryCategoryLevel2) {
        return backendUserDao.categorylevellist2(queryCategoryLevel2);
    }
}
